import random

from plane_constant import *
from plane_sprites import GameSprite


class Enemy(GameSprite):
    """敌机精灵"""

    def __init__(self):

        # 1. 调用父类方法，创建敌机精灵，同时指定敌机图片
        super().__init__("./images/enemy1.png")

        # 2. 指定敌机的初始随机速度 1 ~ 3
        self.speed = random.randint(1, 3)

        # 3. 指定敌机的初始随机位置
        self.rect.bottom = 0

        max_x = SCREEN_RECT.width - self.rect.width
        self.rect.x = random.randint(0, max_x)

    def update(self):

        # 1. 调用父类方法，保持垂直方向的飞行
        super().update ()

        # 2. 判断是否飞出屏幕，如果是，需要从精灵组删除敌机
        if self.rect.y >= SCREEN_RECT.height:
            # print("飞出屏幕，需要从精灵组删除...")
            # kill方法可以将精灵从所有精灵组中移出，精灵就会被自动销毁
            self.kill()

    def __del__(self):
        # print("敌机挂了 %s" % self.rect)
        pass

class SmallEnemy(GameSprite):

    energy = 1
    def __init__(self,bg_size):
        super().__init__("./images/enemy1.png", 0)
        self.destroy_images = []
        self.destroy_images.extend([pygame.image.load("images/enemy1_down1.png"), \
                                    pygame.image.load("images/enemy1_down2.png"), \
                                    pygame.image.load("images/enemy1_down3.png"), \
                                    pygame.image.load("images/enemy1_down4.png"), \
                                    ])
        self.rect = self.image.get_rect()
        self.width,self.height = bg_size[0],bg_size[1]
        self.speed = 2
        self.active = True
        self.mask = pygame.mask.from_surface(self.image)
        self.rect.left,self.rect.top = random.randint(0, self.width - self.rect.width), \
                                       random.randint(-5 * self.height, 0)
        self.energy = SmallEnemy.energy
        self.hit = False

    def move(self):
        #判断是否飞出屏幕，如果是，重置敌机
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.reset()

    def reset(self):
        self.active = True
        self.energy = SmallEnemy.energy
        self.rect.left, self.rect.top = random.randint(0, self.width - self.rect.width), \
                                        random.randint(-5 * self.height, 0)

class MidEnemy(GameSprite):
    energy = 8
    def __init__(self,bg_size):
        super().__init__("./images/enemy2.png", 0)
        self.image_hit = pygame.image.load("images/enemy2_hit.png")
        self.destroy_images = []
        self.destroy_images.extend([pygame.image.load("images/enemy2_down1.png"), \
                                    pygame.image.load("images/enemy2_down2.png"), \
                                    pygame.image.load("images/enemy2_down3.png"), \
                                    pygame.image.load("images/enemy2_down4.png"), \
                                    ])
        self.rect = self.image.get_rect()
        self.width,self.height = bg_size[0],bg_size[1]
        self.rect.left,self.rect.top = random.randint(0, self.width - self.rect.width), \
                                       random.randint(-10 * self.height, -self.height)
        self.speed = 1
        self.active = True
        self.mask = pygame.mask.from_surface(self.image)
        self.energy = MidEnemy.energy
        self.hit = False
    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.reset()

    def reset(self):
        self.active = True
        self.energy = MidEnemy.energy
        self.rect.left, self.rect.top = random.randint(0, self.width - self.rect.width), \
                                        random.randint(-10 * self.height, -self.height)


class BigEnemy(GameSprite):
    energy = 20
    def __init__(self,bg_size):
        super().__init__("./images/enemy3_n1.png", 0)

        self.image1 = pygame.image.load("images/enemy3_n2.png")
        self.image_hit = pygame.image.load("images/enemy3_hit.png")
        self.destroy_images = []
        self.destroy_images.extend([pygame.image.load("images/enemy3_down1.png"), \
                                    pygame.image.load("images/enemy3_down2.png"), \
                                    pygame.image.load("images/enemy3_down3.png"), \
                                    pygame.image.load("images/enemy3_down4.png"), \
                                    pygame.image.load("images/enemy3_down5.png"), \
                                    pygame.image.load("images/enemy3_down6.png"), \
                                    ])
        self.rect = self.image.get_rect()
        self.width,self.height = bg_size[0],bg_size[1]
        self.speed = 1
        self.active = True
        self.mask = pygame.mask.from_surface(self.image)
        self.energy = BigEnemy.energy
        self.hit = False
        self.rect.left,self.rect.top = random.randint(0, self.width - self.rect.width), \
                                       random.randint(-15 * self.height, -5 * self.height)
    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.reset()

    def reset(self):
        self.active = True
        self.energy = BigEnemy.energy
        self.rect.left, self.rect.top = random.randint(0, self.width - self.rect.width), \
                                        random.randint(-15 * self.height, -5*self.height)
